﻿Imports DevExpress.UserSkins
Imports DevExpress.LookAndFeel
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraCharts
Imports System.IO


Public Class frmHome
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        BonusSkins.Register()
        'UserLookAndFeel.Default.SetSkinStyle("Pumpkin")
        LoadGD()
        ' Add any initialization after the InitializeComponent() call.
        SkinHelper.InitSkinGallery(RibbonGalleryBarItem1, True)
        'lbNoiDung.Text = "Galaxy Tab 2 7.0 được trang bị kết nối 3G, WiFi, có 2 camera trong đó camera chính 3,15 MP có khả năng nhận dạng nụ cười, quay phim HD 1080p. Thiết bị cũng được trang bị các loại cảm biến: con quay hồi chuyển, cảm biến gia tốc, cảm biến ánh sáng, cảm biến la bàn số."
        'LoadND()
    End Sub
    Public Shared flagEdit As Boolean

#Region "Sự Kiện"

    Private Sub XtraForm1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dnLich.DateTime = Date.Today
        LoadND()
        LoadChart()
    End Sub

    Private Sub dnLich_Click(sender As System.Object, e As System.EventArgs) Handles dnLich.Click
        dnLich.DateTime = dnLich.Selection.Item(0)
        'MessageBox.Show(dnLich.DateTime.ToString)
        LoadND()
    End Sub


    Private Sub bntSua_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles bntSua.ItemClick
        Dim nhk As New NhatKiDTO
        nhk.Ngay = dnLich.DateTime.ToShortDateString()
        If (NhatKiBUS.Check(nhk)) Then
            flagEdit = True
            VietNhatKi.ShowDialog()
        Else
            lbNoiDung.Text = "Không có nội dung để sửa, không thể tiến hành thao tác này"
            lbNoiDung.ForeColor = Color.Red
        End If
    End Sub


    Private Sub XtraForm1_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Application.Exit()
    End Sub


    Private Sub bntThem_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles bntThem.ItemClick
        Dim nhk As New NhatKiDTO
        nhk.Ngay = Date.Today
        If (NhatKiBUS.Check(nhk)) Then
            flagEdit = True
            lbNoiDung.Text = NhatKiBUS.LoadND(nhk).Rows(0)(0)
            VietNhatKi.ShowDialog()
        Else
            flagEdit = False
            Me.Hide()
            VietNhatKi.ShowDialog()
        End If
    End Sub

    Private Sub XtraForm1_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim users As New UsersDTO
        users.giaoDien = UserLookAndFeel.Default.ActiveSkinName
        UsersBUS.UpdateGD(users)
    End Sub

    Private Sub bntBackup_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles bntBackup.ItemClick
        Backup()
    End Sub

    Private Sub bntRestore_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles bntRestore.ItemClick
        Restore()
    End Sub

    Private Sub bntExit_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles bntExit.ItemClick
        Application.Exit()
    End Sub

    Private Sub bntDoiMatKhau_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles bntDoiMatKhau.ItemClick
        Dim frm As New frmDoiMatKhau
        frm.ShowDialog()
    End Sub

#End Region


#Region "Hàm Xử Lí"
    Public Sub LoadGD()
        Dim dt As DataTable
        dt = UsersBUS.LoadGD()
        If (dt.Rows.Count > 0) Then
            UserLookAndFeel.Default.SetSkinStyle(dt.Rows(0)(0).ToString())
        End If
    End Sub
    Public Sub LoadND()
        If (dnLich.Selection.Count > 0) Then
            Dim dt As New DataTable
            Dim nhk As New NhatKiDTO
            nhk.Ngay = dnLich.Selection.Item(0).ToShortDateString()
            ' MessageBox.Show(dnLich.Selection.Item(0).ToShortDateString())
            dt = NhatKiBUS.LoadND(nhk)
            If (dt.Rows.Count > 0) Then
                lbNoiDung.Text = dt.Rows(0)(0).ToString()
                lbNoiDung.ForeColor = Color.Black
            Else
                lbNoiDung.Text = "Không Có Nội Dung"
                lbNoiDung.ForeColor = Color.Red
            End If
        End If
    End Sub

    Public Sub Restore()
        Try
            ofdRestore.ShowDialog()
            Dim file As New FileInfo(ofdRestore.FileName)
            file.CopyTo(Application.StartupPath + "\Database.mdb", True)
            MessageBox.Show("Restore Thành Công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Catch ex As Exception
            If (MessageBox.Show("Lỗi, Hãy Thử Lại", "Báo Lỗi", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) = DialogResult.Retry) Then
                Restore()
            Else
                Return
            End If
        End Try
    End Sub

    Public Sub Backup()
        Try
            fbdBackUp.ShowDialog()
            Dim file As New FileInfo(Application.StartupPath + "\Database.mdb")
            file.CopyTo(fbdBackUp.SelectedPath + "\" + file.Name, True)
            MessageBox.Show("Backup Thành Công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Catch ex As Exception
            If (MessageBox.Show("Lỗi, Hãy Thử Lại", "Báo Lỗi", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) = DialogResult.Retry) Then
                Backup()
            Else
                Return
            End If
        End Try
    End Sub

    Public Sub LoadChart()
        If (ccThoiGian.Series.Count > 0) Then
            ccThoiGian.BeginInit()
            Dim series As Series
            series = ccThoiGian.Series(0)
            series.Points.Clear()
            Dim dt As DataTable
            dt = NhatKiBUS.LoadThoiGian()
            For i As Integer = 0 To 2
                series.Points.Add(New SeriesPoint(i + 1, dt.Rows(0)(i)))
            Next
            series.Points(0).Argument = "Hữu Ích"
            series.Points(1).Argument = "Không Hữu Ích"
            series.Points(2).Argument = "Không Tên"
            ccThoiGian.EndInit()
        End If
    End Sub
#End Region

End Class